import requests
from lxml import etree
import urllib.request


def img_list():
    #爬取的url
    url="https://www.vcg.com/creative-image/2436970"
    #浏览器Agent
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
    }
    response = requests.get(url=url, headers=headers)
    response.encoding = "utf-8"
    #实例化解析器
    tree = etree.HTML(response.text)
    """
    //section/div/figure/a/img
    """
    img_list = tree.xpath("//section/div/figure/a/img")
    #分析当前页面的image
    for i in img_list:
        name = "http:"+i.xpath("./@data-src")[0]
        src = i.xpath("./@title")[0]
        filename = "./img_12/" + src+".jpg"
        #下载到当前目录的img_12
        #在当前目录创建img_12文件夹！！！
        urllib.request.urlretrieve(url=name, filename=filename)
        print(filename + "----" + src)



if __name__ == '__main__':
    #开爬
    img_list()